<script setup lang="ts">
import {ElMessage} from "element-plus";
defineOptions({name:'SkuForm'})
import {reactive, ref} from "vue";
const $emit = defineEmits(['changeScene'])
import {reqAttr} from '@/api/product/attr/index.ts'
import {reqSpuImageList, reqSpuHasSaleAttr, reqAddSku} from '@/api/product/spu/index.ts'
import type {SpuData,SkuData} from '@/api/product/spu/type.ts'
/**
 * 取消编辑，返回主页面
 */
const cancel = () => {
  $emit('changeScene',{flag:0,params:''})
}
const attrArr= ref<any>([]) //平台属性
const saleArr= ref() //销售属性
const imgArr= ref() //照片墙的数据
//收集sku参数
const skuParams=reactive<SkuData>({
  category3Id: '',
  spuId: 0,
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [
    {
      attrId: '',
      valueId: ''
    }
  ],
  skuSaleAttrValueList: [
    {
      saleAttrId: '',
      saleAttrValueId: ''
    }
  ],
  skuDefaultImg: ''
})
/**
 *初始化数据
 */
const initSkuData = async (c1Id:string|number,c2Id:string|number,spu:SpuData) => {
  skuParams.category3Id=spu.category3Id
  skuParams.spuId=spu.id as number
  skuParams.tmId=spu.tmId
  //平台属性
  const res = await reqAttr(c1Id,c2Id,spu.category3Id)
  attrArr.value=res.data
  //销售属性
  const res2 = await reqSpuHasSaleAttr(spu.id as number)
  saleArr.value=res2.data
  //照片墙的数据
  const res3 = await reqSpuImageList(Number(spu.id))
  imgArr.value=res3.data
}
defineExpose({initSkuData})
const table= ref<any>()
/**
 * 收集默认图片
 * @param row
 */
const handler = (row:any) => {
  imgArr.value.forEach((item:any)=>{
    table.value?.toggleRowSelection(item,false)
  })
  table.value?.toggleRowSelection(row,true)
  skuParams.skuDefaultImg=row.imgUrl
}
/**
 * 保存
 */
const save = async () => {
  skuParams.skuAttrValueList=attrArr.value.reduce((prev:any,next:any)=>{
    if(next.attrIdAndValueId){
      const [attrId,valueId] = next.attrIdAndValueId.split(':')
      prev?.push({
        attrId,valueId
      })
    }
    return prev
  },[])
  skuParams.skuSaleAttrValueList=saleArr.value.reduce((prev:any,next:any)=>{
    if(next.saleIdAndValueId){
      const [saleAttrId,saleAttrValueId] = next.saleIdAndValueId.split(':')
      prev.push({
        saleAttrId,saleAttrValueId
      })
    }
    return prev
  },[])
  const res:any = await reqAddSku(skuParams)
  if(res.code==200){
    ElMessage({
      type:'success',
      message:'添加SKU成功'
    })
    $emit('changeScene',{flag:0,params:''})
  }else{
    ElMessage({
      type:'error',
      message:'添加SKU失败'
    })
  }
}
</script>

<template>
  <el-form label-width="120px">
    <el-form-item label="SKU名称" >
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input placeholder="SKU价格" type="number" v-model="skuParams.price"></el-input>
    </el-form-item >
    <el-form-item label="重量(克)">
      <el-input placeholder="SKU价格" type="number" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input placeholder="SKU价格" type="textarea" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form inline label-width="80px">
        <el-form-item :label="item.attrName" v-for="(item) in attrArr" :key="item.id" style="width: 30%;margin-bottom: 8px;">
          <el-select placeholder="请选择" v-model="item.attrIdAndValueId">
            <el-option :value="`${item.id}:${attrValue.id}`"
                       :label="attrValue.valueName"
                       v-for="(attrValue) in item.attrValueList"
                       :key="attrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form inline>
        <el-form-item :label="item.saleAttrName" v-for="(item) in saleArr" :key="item.id">
          <el-select placeholder="请选择" v-model="item.saleIdAndValueId">
            <el-option v-for="(saleAttrValue) in item.spuSaleAttrValueList"
                       :value="`${item.id}:${saleAttrValue.id}`"
                       :key="saleAttrValue.id"
                       :label="saleAttrValue.saleAttrValueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table :data="imgArr" border ref="table">
        <el-table-column type="selection" title="序号" width="80px" align="center"></el-table-column>
        <el-table-column label="图片">
          <template #default="{row}">
            <el-image :src="row.imgUrl" alt="" style="width:100px;height:100px;"/>
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="warning" icon="Edit" size="small" @click="handler(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item label="操作">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="primary" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss">

</style>